package com.dfiles.view.activity;

import java.io.Serializable;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.dfiles.Config;
import com.dfiles.R;
import com.dfiles.bean.DFile;
import com.dfiles.provider.FileTypeHelper.FileTypeAll;
import com.dfiles.utils.UIHelper;

/**
 * 
 * @Description: 主界面
 */
public class ActDFiles extends BaseActivity implements OnClickListener {
	public static final String TAG = ActDFiles.class.getName();
	private Context context;
	private View vFile;
	private View vDoc;
	private View vImg;
	private View vVideo;
	private View vAudio;
	private View vApp;
	List<DFile> selected = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.context = this;
		/* 在content前添加 */
		// UIHelper.setFullScreen(this);
		setContentView(R.layout.act_dfiles);
		// Window dialogWindow = getWindow();
		// dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
		setFinishOnTouchOutside(false);
		initView();
		initListener();
	}

	public void initView() {
		vFile = findViewById(R.id.file);
		vDoc = findViewById(R.id.doc);
		vImg = findViewById(R.id.img);
		vVideo = findViewById(R.id.video);
		vAudio = findViewById(R.id.audio);
		vApp = findViewById(R.id.app);
	}

	// 初始化listener
	private void initListener() {
		vFile.setOnClickListener(typeListener);
		vDoc.setOnClickListener(typeListener);
		vImg.setOnClickListener(typeListener);
		vVideo.setOnClickListener(typeListener);
		vAudio.setOnClickListener(typeListener);
		vApp.setOnClickListener(typeListener);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		default:
			break;
		}
	}

	/**
	 * 类别监听器
	 */
	private OnClickListener typeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			FileTypeAll fileType = null;
			switch (v.getId()) {
			case R.id.file:
				fileType = FileTypeAll.FILE_TYPE_FOLDER;
				break;
			case R.id.doc:
				fileType = FileTypeAll.FILE_TYPE_DOC;
				break;
			case R.id.img:
				fileType = FileTypeAll.FILE_TYPE_IMAGE;
				break;
			case R.id.video:
				fileType = FileTypeAll.FILE_TYPE_VIDEO;
				break;
			case R.id.audio:
				fileType = FileTypeAll.FILE_TYPE_AUDIO;
				break;
			case R.id.app:
				fileType = FileTypeAll.FILE_TYPE_APP;
				break;
			default:
				fileType = FileTypeAll.FILE_TYPE_FOLDER;
				break;
			}
			Bundle bun = new Bundle();
			bun.putSerializable(ActAllFiles.TYPEKEY, fileType);
			bun.putInt("flagViewType", Config.UI.FLAG_VIEW_GRID);
			Intent intent = new Intent();
			intent.putExtras(bun);
			intent.setClass(context, ActAllFiles.class);
			startActivityForResult(intent, Config.FLAG_REQUESTCODE);
			overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
		}
	};

	/**
	 * 
	 * @Title: back2Main
	 * @Description: 返回主界面
	 * @param
	 * @return void
	 * @throws
	 */
	private void back2Main() {
		finish();
		if (onOperationListener != null) {
			onOperationListener.back(true);
		}
	}

	/**
	 * 点击返回按钮监听
	 */
	private static OnOperationListener onOperationListener;

	public static void setOnOperationListener(OnOperationListener onOperationListener) {
		ActDFiles.onOperationListener = onOperationListener;
	}

	public interface OnOperationListener {
		void back(boolean needReload);
	}

	/**
	 * 
	 * @Description: back key pressed
	 * @param
	 * @return void
	 * @throws
	 */

	@Override
	public void finish() {
		super.finish();
	}

	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle("返回");
		getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
		// actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
		// ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
		return true;
	}

	// 菜单项被选中时执行该方法
	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		if (menuItem.isCheckable()) {
			menuItem.setChecked(true);
		}
		switch (menuItem.getItemId()) {
		case android.R.id.home:
			// 点击到我们ActionBar中设置的Home按钮
			back2Main();
			break;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (requestCode) {
		case Config.FLAG_REQUESTCODE:
			if (resultCode == RESULT_OK) {
				if (data != null) {
					selected = (List<DFile>) data.getSerializableExtra(Config.FLAG_RESULT_DATA);
					if (selected != null) {
						UIHelper.makeToast(context, "已经选中了：" + selected.size());
						// 将选中的文件传回到调用处
						Intent intent = new Intent();
						intent.putExtra(Config.FLAG_RESULT_DATA, (Serializable) selected);
						setResult(Activity.RESULT_OK, intent);
					}
				}
			}
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
